package com.likou;

import java.util.Arrays;

public class q416 {

    public boolean canPartition(int[] nums) {

        int sum = 0;
        int max = 0;
        for (int i = 0; i < nums.length; i++) {
            max = Math.max(max, nums[i]);
            sum += nums[i];
        }

        if (sum % 2 != 0) {
            return false;
        }

        int target = sum / 2;
        if (max > target) {
            return false;
        }

        boolean[] dp = new boolean[target + 1];
        dp[0] = true;
        for (int i = 0; i < nums.length; i++) {
            for (int j = target; j >= nums[i]; j--) {
                if (dp[j-nums[i]]){
                    dp[j] = true;
                }

            }
        }

        return dp[target];

    }

}
